Shader "Shiku/AvaterUI"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        //[HDR]_Color ("颜色",color) = (1,1,1,1)
        _MaskX ("MaskX", Range(0,1)) = 0.5
        _MaskY ("MaskY", Range(0,1)) = 0.5
        _VignetteExtend ("暗角扩展", Range(0,0.5)) = 0.2
        _VignetteBlur ("暗角羽化", Range(0.001,0.5)) = 0.1
        _IsSphere ("圆型", Range(0,1)) = 0
    }
    SubShader
    {
        Cull Off
		Lighting Off
		ZWrite Off
		ZTest [unity_GUIZTestMode]
		Blend SrcAlpha OneMinusSrcAlpha
        Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "PreviewType"="Plane" "CanUseSpriteAtlas"="True" }

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            sampler2D _Mask;
            float4 _MainTex_ST;
            float4 _Mask_ST;
            half _MaskX,_MaskY,_VignetteExtend,_VignetteBlur,_IsSphere;
            //fixed4 _Color;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            half SphereMask(half baseColor ,half size ,half SoftEdge)
            {
                return 1/pow(2,saturate((1-size)-saturate(baseColor))*(1/SoftEdge));
            }

            half BoxMask(half baseColor ,half size ,half SoftEdge)
            {
                return 1/pow(2,saturate((1-size)-saturate(baseColor))*(1/SoftEdge));
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex, i.uv);

                half spheremask = 1-saturate(SphereMask(distance(i.uv,half2(_MaskX,_MaskY)),0.5+_VignetteExtend,_VignetteBlur));
                half boxMask = 1-saturate(BoxMask(distance(i.uv.x,_MaskX),0.5+_VignetteExtend,_VignetteBlur));
                if(i.uv.y < _MaskY)
                {
                    boxMask = 0;
                }
                half mask = spheremask;
                if(i.uv.y > _MaskY)
                {
                    mask = 0;
                }
                half maskA = lerp(saturate(boxMask+mask),spheremask,_IsSphere);

                return fixed4(col.rgb,col.a*maskA);
            }
            ENDCG
        }
    }
}
